package com.bj58.ecdata.dashboard.utils;

import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

public class NetFlowABTestUitls {

	/**
	 * @description 按标准时间间隔（5分钟）对数据进行补充操作
	 * @param list 按时间排序的对象集合
	 * @return 返回一个含有288个点的对象集合
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static <T> Collection<T> exchange(List<T> list,Class<T> entityClass) throws Exception{
		Collection<T> collection=Collections.emptyList();
		if(list!=null&&list.size()!=0){
			T lastBean=list.get(list.size()-1);
			String tmpHour=(String)ReflectUtil.getVal(lastBean, "stat_hour");
			String tmpMinutes=(String)ReflectUtil.getVal(lastBean, "stat_min");
			Map<String , T> map=initMap(tmpHour,tmpMinutes,entityClass);
			for(T bean:list){
				String hour=(String)ReflectUtil.getVal(bean, "stat_hour");
				String minutes=(String)ReflectUtil.getVal(bean, "stat_min");
				String tmpKey=hour+minutes;
				map.put(tmpKey, bean);
			}
			collection=map.values();
		}
		return collection;
	}
	
	//先初始化出一个时间map
	private static <T> Map<String , T> initMap(String hour,String minitues,Class<T> entityClass) throws InstantiationException, IllegalAccessException, ParseException{
		Map<String , T> map=new TreeMap<String, T>();
		Date initialDate =  DateUtils.parseDate("00:05", "HH:mm");
		Date tmpdate =  DateUtils.parseDate("00:05", "HH:mm");
		while(true){
			String dateF=DateFormatUtils.format(tmpdate, "dd-HH-mm");
			String[] dateFs=dateF.split("-");
			if(DateUtils.isSameDay(initialDate, tmpdate)){
				T tmpBean = entityClass.newInstance();
				String tmpHours=dateFs[1];
				String tmpMinutes=dateFs[2];
				String tmpKey=dateFs[1]+dateFs[2];
				map.put(tmpKey, tmpBean);
				if(tmpHours.equals(hour) && tmpMinutes.equals(minitues))
					break;
				tmpdate = DateUtils.addMinutes(tmpdate, 5);
				
			}else{
				T tmpBean = entityClass.newInstance();
				String tmpKey="2400";
				map.put(tmpKey, tmpBean);
				break;
			}
		}
		return map;
	}


}
